package com.td.filemanager.business.device;

import com.td.filemanager.MyApplication;

import com.td.lib.viewmodel.ViewModel;
import com.td.library.data.DeviceData;
import com.td.library.utils.LogUtil;
import ohos.aafwk.abilityjet.activedata.ActiveData;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备管理ViewModel
 */
public class DeviceManagerViewModel extends ViewModel {
    private static final String TAG = DeviceManagerViewModel.class.getSimpleName();

    private ActiveData<List<DeviceData>> mDevicedata = new ActiveData<>();
    private ActiveData<Boolean> mSelectAll = new ActiveData<>();

    public DeviceManagerViewModel() {
        refresh();
    }


    /**
     * 刷新
     */
    public void refresh() {
        LogUtil.info(TAG, "start refresh init");

        List<DeviceData> deviceDataList = mDevicedata.getData();
        if (deviceDataList == null) {
            deviceDataList = new ArrayList<>();
        }
        deviceDataList.clear();
        
        ohos.data.distributed.device.DeviceInfo localDeviceInfo = KvManagerFactory.getInstance()
                .createKvManager(new KvManagerConfig(MyApplication.getInstance())).getLocalDeviceInfo();
        DeviceData local = new DeviceData(true, localDeviceInfo.getName(),
                localDeviceInfo.getId());
        deviceDataList.add(local);

        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        for (DeviceInfo remote : deviceInfoList) {
            deviceDataList.add(new DeviceData(false, remote.getDeviceName(),
                    remote.getDeviceId()));
        }
        LogUtil.info(TAG, "get " + deviceInfoList.size() + " device info " + deviceDataList);
        mDevicedata.setData(deviceDataList);
    }

    /**
     * 获取设备列表数据
     *
     * @return 设备列表数据
     */
    public ActiveData<List<DeviceData>> getDevicedata() {
        return mDevicedata;
    }

    /**
     * 获取所有选择的数据
     *
     * @return 所有选择的数据
     */
    public ActiveData<Boolean> getSelectAll() {
        return mSelectAll;
    }

    /**
     * 是否全选
     *
     * @return 是否全选
     */
    public boolean isAllItemSelected() {
        return mDevicedata.getData().stream().allMatch(DeviceData::isChecked);
    }

    /**
     * 是否全不选
     *
     * @return 是否全不选
     */
    public boolean isAllItemUnSelected() {
        return mDevicedata.getData().stream().noneMatch(DeviceData::isChecked);
    }

    /**
     * 获取设备列表数据
     *
     * @return 设备列表数据
     */
    public List<DeviceData> getSelectedDevices() {
        return mDevicedata.getData().stream()
                .filter(DeviceData::isChecked)
                .collect(Collectors.toList());
    }

    /**
     * 刷新item中的checkbox的状态
     *
     * @param position position
     * @param isChecked isChecked
     */
    public void refreshItemCbState(int position, boolean isChecked) {
        if (getDevicedata().getData() == null ) {
            LogUtil.warn(TAG, "refreshItemCbState null");
            return;
        }
        List<DeviceData> data = getDevicedata().getData();
        try {
            data.get(position).setChecked(isChecked);
            getDevicedata().setData(data);
        } catch (ArrayIndexOutOfBoundsException e) {
            LogUtil.warn(TAG, "onItemCbCheckedChangedListener index out of bounds");
        }
    }
}
